package com.qindi.db;

import java.sql.*;

import com.qindi.mvc.utility.Global;

public class DbControl{
	/* =====================================================================
	 * 	Connect to database
	 * ---------------------------------------------------------------------*/
	public static Connection getConnection() throws Exception // NOPMD by Administrator on 6/16/09 8:29 PM
	{			
		try{			
	        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
			String url = "jdbc:sqlserver://"+Global.sqlServer+";database="+Global.database+";useUnicode=true;characterEncoding=UTF-8";	        	        			
			return (Connection) DriverManager.getConnection(url,Global.dbUserName,Global.password);	    			
		}
		catch(Exception e){		
			e.printStackTrace();
			throw new Exception("Error occured while connecting to database. Please try again later.");
		}	
	}
	/* =====================================================================
	 * 	Connect to database with arguments
	 * ---------------------------------------------------------------------*/
	public static Connection getConnection(String location , String db ,  String username , String password) throws Exception
	{		
		try{
			String url = "jdbc:sqlserver://"+location+";database="+db+";useUnicode=true;characterEncoding=UTF-8";	        
			return (Connection) DriverManager.getConnection(url,username,password);	
		}
		catch(Exception e){
			e.printStackTrace();
			throw new Exception("Could not get connection to database. Exception = " + e.toString());
		}	
	}
	/* =====================================================================
	 * 	Check database connection
	 * ---------------------------------------------------------------------*/
	public static boolean checkDbConnection() throws Exception
	{							
	    return    DbControl.checkDbConnection(Global.sqlServer, Global.database, Global.dbUserName, Global.password);
	}
	/* =====================================================================
	 * 	Check database connection
	 * ---------------------------------------------------------------------*/
	public static boolean checkDbConnection(String location , String db ,  String username , String password) throws Exception
	{					
		try{
	        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
			String url = "jdbc:sqlserver://"+location+";database="+db+";useUnicode=true;characterEncoding=UTF-8";	        
			Connection con = (Connection) DriverManager.getConnection(url,username,password);
			con.close();
		}
		catch(Exception e){	return false; }	
		return true;
	}
	/* =====================================================================
	 * 	Unit Test
	 * ---------------------------------------------------------------------*/
	public static void main(String[] args) {

			try {
				/*Connection con = DbControl.getConnection();
				Statement stmt = null;
				ResultSet	 r = null;
				stmt=(Statement) con.createStatement();
				r = (ResultSet) stmt.executeQuery("SELECT * FROM services");

				while(r.next())			
					System.out.println((String)(r.getObject(2))+"----- " + (r.getBoolean(3)) );
				
				stmt.close();
				con.close();
*/							
			//System.out.println("Check db - " + DbControl.checkDbConnection("localhost\\SQLEXPRESS", "AutoAtt", "sa", "sa123456"));								
			} catch (Exception e) {
				e.printStackTrace();
			}
	}
	// TODO need to depreciate checkDbConnection(), instead use an entry in qindi.config.xml that points to user defined method to check connectivity
	/**
	 * Check database connectivity.
	 * 
	 * @deprecated
	 * @param	location	Location URL of database
	 * @param	db			Name of database
	 * @param	username	Database user name
	 * @param	password	Database password
	 * @return				<code>true</code> if the image is completely loaded and was painted successfully; <code>false</code> otherwise.
	 */
	public boolean checkDbConnection2(String location , String db ,  String username , String password) throws Exception
	{					
		try{
	        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
			String url = "jdbc:sqlserver://"+location+";database="+db+";useUnicode=true;characterEncoding=UTF-8";	        
			Connection con = (Connection) DriverManager.getConnection(url,username,password);
			con.close();
		}
		catch(Exception e){	
			//logger.error(e + ".Params : " + location + " , " + db + " , " + username + " , " + password);
			e.printStackTrace(); 
			return false; }	
		return true;
	}
} 
